﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using UnityEngine;

public static class CollectionsExtension
{
    [CompilerGenerated]
    private static Guid <DisruptTheOrder`1>m__15F<T>(T x)
    {
        return Guid.NewGuid();
    }

    [CompilerGenerated]
    private static <>__AnonType2<T, int> <FindAllIndices`1>m__160<T>(T v, int i)
    {
        return new <>__AnonType2<T, int>(v, i);
    }

    [CompilerGenerated]
    private static int <FindAllIndices`1>m__162<T>(<>__AnonType2<T, int> x)
    {
        return x.index;
    }

    [CompilerGenerated]
    private static float <GetRandomElementByWeight`1>m__15E<T>(<>__AnonType1<T, float> x)
    {
        return x.weight;
    }

    [CompilerGenerated]
    private static int <GetRandomElements`1>m__15C<T>(T x, T y)
    {
        return UnityEngine.Random.Range(-1, 2);
    }

    public static void AddOrSet<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key, TValue value)
    {
        if (!dic.ContainsKey(key))
        {
            dic.Add(key, value);
        }
        else
        {
            dic[key] = value;
        }
    }

    public static List<T> DisruptTheOrder<T>(this List<T> list)
    {
        return list.OrderBy<T, Guid>(new Func<T, Guid>(CollectionsExtension.<DisruptTheOrder`1>m__15F<T>)).ToList<T>();
    }

    public static T Find<T>(this IList<T> list, Predicate<T> predicate)
    {
        for (int i = 0; i < list.Count; i++)
        {
            T local = list[i];
            if (local != null)
            {
                return local;
            }
        }
        return default(T);
    }

    public static int[] FindAllIndices<T>(this List<T> list, Func<T, bool> predicate)
    {
        <FindAllIndices>c__AnonStorey7F<T> storeyf = new <FindAllIndices>c__AnonStorey7F<T> {
            predicate = predicate
        };
        return list.Select<T, <>__AnonType2<T, int>>(new Func<T, int, <>__AnonType2<T, int>>(CollectionsExtension.<FindAllIndices`1>m__160<T>)).Where<<>__AnonType2<T, int>>(new Func<<>__AnonType2<T, int>, bool>(storeyf.<>m__161)).Select<<>__AnonType2<T, int>, int>(new Func<<>__AnonType2<T, int>, int>(CollectionsExtension.<FindAllIndices`1>m__162<T>)).ToArray<int>();
    }

    public static int FindIndex<T>(this IList<T> list, Predicate<T> predicate)
    {
        for (int i = 0; i < list.Count; i++)
        {
            if (predicate(list[i]))
            {
                return i;
            }
        }
        return -1;
    }

    public static void Foreach<T>(this IEnumerable<T> array, Action<T> doSomething)
    {
        IEnumerator<T> enumerator = array.GetEnumerator();
        try
        {
            while (enumerator.MoveNext())
            {
                T current = enumerator.Current;
                doSomething(current);
            }
        }
        finally
        {
            if (enumerator == null)
            {
            }
            enumerator.Dispose();
        }
    }

    public static TSource GetElement<TSource>(this IList<TSource> list, int index)
    {
        if ((index >= 0) && (index < list.Count))
        {
            return list[index];
        }
        return default(TSource);
    }

    public static T GetRandomElement<T>(this IEnumerable<T> enumerable)
    {
        if ((enumerable == null) || (enumerable.Count<T>() == 0))
        {
            return default(T);
        }
        int index = UnityEngine.Random.Range(0, enumerable.Count<T>());
        return enumerable.ElementAt<T>(index);
    }

    public static T GetRandomElementByWeight<T>(this IEnumerable<T> enumerable, Func<T, float> weightSelector)
    {
        <GetRandomElementByWeight>c__AnonStorey7E<T> storeye = new <GetRandomElementByWeight>c__AnonStorey7E<T> {
            weightSelector = weightSelector
        };
        T randomElement = enumerable.GetRandomElement<T>();
        IEnumerable<<>__AnonType1<T, float>> source = enumerable.Select<T, <>__AnonType1<T, float>>(new Func<T, <>__AnonType1<T, float>>(storeye.<>m__15D));
        float max = source.Sum<<>__AnonType1<T, float>>(new Func<<>__AnonType1<T, float>, float>(CollectionsExtension.<GetRandomElementByWeight`1>m__15E<T>));
        float num2 = UnityEngine.Random.Range(0f, max);
        float num3 = 0f;
        IEnumerator<<>__AnonType1<T, float>> enumerator = source.GetEnumerator();
        try
        {
            while (enumerator.MoveNext())
            {
                <>__AnonType1<T, float> current = enumerator.Current;
                if ((num3 < num2) && ((num3 + current.weight) > num2))
                {
                    randomElement = current.value;
                }
                num3 += current.weight;
            }
        }
        finally
        {
            if (enumerator == null)
            {
            }
            enumerator.Dispose();
        }
        return randomElement;
    }

    public static IEnumerable<T> GetRandomElements<T>(this IEnumerable<T> enumerable, int count)
    {
        if ((enumerable == null) || (enumerable.Count<T>() == 0))
        {
            return null;
        }
        List<T> source = enumerable.ToList<T>();
        source.Sort(new Comparison<T>(CollectionsExtension.<GetRandomElements`1>m__15C<T>));
        return source.Take<T>(count);
    }

    public static void RemoveAll<T>(this ICollection<T> collection, Func<T, bool> predicate)
    {
        for (int i = 0; i < collection.Count; i++)
        {
            T local = collection.ElementAt<T>(i);
            if (predicate(local))
            {
                collection.Remove(local);
                i--;
            }
        }
    }

    public static TResult SelectFirst<TSource, TResult>(this IEnumerable<TSource> enumerable, Func<TSource, TResult> selector)
    {
        IEnumerator<TSource> enumerator = enumerable.GetEnumerator();
        try
        {
            while (enumerator.MoveNext())
            {
                TSource current = enumerator.Current;
                TResult local2 = selector(current);
                if (local2 != null)
                {
                    return local2;
                }
            }
        }
        finally
        {
            if (enumerator == null)
            {
            }
            enumerator.Dispose();
        }
        return default(TResult);
    }

    private static void Shift<T>(this T[] array, int oldIndex, int newIndex)
    {
        if (oldIndex != newIndex)
        {
            T local = array[oldIndex];
            if (newIndex < oldIndex)
            {
                Array.Copy(array, newIndex, array, newIndex + 1, oldIndex - newIndex);
            }
            else
            {
                Array.Copy(array, oldIndex + 1, array, oldIndex, newIndex - oldIndex);
            }
            array[newIndex] = local;
        }
    }

    public static void ShiftLeft<T>(this T[] array)
    {
        array.ShiftLeft<T>(0, array.Length);
    }

    public static void ShiftLeft<T>(this T[] array, int startIndex, int length)
    {
        array.Shift<T>(startIndex, (startIndex + length) - 1);
    }

    public static void ShiftRight<T>(this T[] array)
    {
        array.ShiftRight<T>(0, array.Length);
    }

    public static void ShiftRight<T>(this T[] array, int startIndex, int length)
    {
        array.Shift<T>((startIndex + length) - 1, startIndex);
    }

    public static IList<T> Swap<T>(this IList<T> list, int indexA, int indexB)
    {
        T local = list[indexA];
        list[indexA] = list[indexB];
        list[indexB] = local;
        return list;
    }

    [DebuggerHidden]
    public static IEnumerable<object> ToEnumerable(this Array array)
    {
        return new <ToEnumerable>c__Iterator17 { array = array, <$>array = array, $PC = -2 };
    }

    [CompilerGenerated]
    private sealed class <FindAllIndices>c__AnonStorey7F<T>
    {
        internal Func<T, bool> predicate;

        internal bool <>m__161(<>__AnonType2<T, int> x)
        {
            return this.predicate(x.value);
        }
    }

    [CompilerGenerated]
    private sealed class <GetRandomElementByWeight>c__AnonStorey7E<T>
    {
        internal Func<T, float> weightSelector;

        internal <>__AnonType1<T, float> <>m__15D(T x)
        {
            return new <>__AnonType1<T, float>(x, this.weightSelector(x));
        }
    }

    [CompilerGenerated]
    private sealed class <ToEnumerable>c__Iterator17 : IDisposable, IEnumerator, IEnumerable, IEnumerator<object>, IEnumerable<object>
    {
        internal object $current;
        internal int $PC;
        internal Array <$>array;
        internal IEnumerator <$s_124>__0;
        internal object <element>__1;
        internal Array array;

        [DebuggerHidden]
        public void Dispose()
        {
            uint num = (uint) this.$PC;
            this.$PC = -1;
            switch (num)
            {
                case 1:
                    try
                    {
                    }
                    finally
                    {
                        IDisposable disposable = this.<$s_124>__0 as IDisposable;
                        if (disposable == null)
                        {
                        }
                        disposable.Dispose();
                    }
                    break;
            }
        }

        public bool MoveNext()
        {
            uint num = (uint) this.$PC;
            this.$PC = -1;
            bool flag = false;
            switch (num)
            {
                case 0:
                    this.<$s_124>__0 = this.array.GetEnumerator();
                    num = 0xfffffffd;
                    break;

                case 1:
                    break;

                default:
                    goto Label_00AA;
            }
            try
            {
                while (this.<$s_124>__0.MoveNext())
                {
                    this.<element>__1 = this.<$s_124>__0.Current;
                    this.$current = this.<element>__1;
                    this.$PC = 1;
                    flag = true;
                    return true;
                }
            }
            finally
            {
                if (!flag)
                {
                }
                IDisposable disposable = this.<$s_124>__0 as IDisposable;
                if (disposable == null)
                {
                }
                disposable.Dispose();
            }
            this.$PC = -1;
        Label_00AA:
            return false;
        }

        [DebuggerHidden]
        public void Reset()
        {
            throw new NotSupportedException();
        }

        [DebuggerHidden]
        IEnumerator<object> IEnumerable<object>.GetEnumerator()
        {
            if (Interlocked.CompareExchange(ref this.$PC, 0, -2) == -2)
            {
                return this;
            }
            return new CollectionsExtension.<ToEnumerable>c__Iterator17 { array = this.<$>array };
        }

        [DebuggerHidden]
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.System.Collections.Generic.IEnumerable<object>.GetEnumerator();
        }

        object IEnumerator<object>.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }

        object IEnumerator.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }
    }
}

